<?php

/**
 * 
 * 生活服务前台控制器
 *  
 */
class ServiceAction extends HomeAction {

    public function _initialize() {
        parent::_initialize();
        $this->seo('生活服务 ');
    }

    function index() {
        //分类
        $cat = D('ServiceCat')
                ->where(array('status' => 1))
                ->select();
        //所带参数
        $cid     = format_int($_REQUEST['cid']);
        //生活服务信息
        $mod     = M('Service');
        $map     = array();
        if ($cid) {
            $map['cat_ids'] = $cid;
        }
        $order          = ' create_time DESC ';
        $limit          = 10;
        //取得满足条件的记录数
        $count          = $mod->where($map)->count($mod->getPk());
        if ($count > 0) {
            import("ORG.Util.Page");
            //创建分页对象
            $p = new Page($count, $limit);
            //分页查询数据

            $voList = $mod
                    ->join('AS s JOIN ' . C('DB_PREFIX') . 'service_cat as c ON c.cat_id=s.cat_ids ')
                    ->join(C('DB_PREFIX') . 'card_theme as ct ON s.theme_id=ct.theme_id')
                    ->where($map)
                    ->order($order)
                    ->field('s.*, c.title as cat_name, ct.*')
                    ->limit($p->firstRow . ',' . $p->listRows)
                    ->select();
            //分页显示
            $page   = $p->show();
            //模板赋值显示
            $data   = array(
                'list'      => $voList,
                'page'      => $page,
                'nowPage'   => $_REQUEST['page'],
                'totalPage' => round($count / $limit),
            );
        }
        //推荐信息
        $roundDZ    = _roundDazhe();
        $this->assign('rounddz', $roundDZ);
        $this->assign('data', $data);
        $this->assign('cat', $cat);
        $this->display();
    }

    public function show() {
        $sid = $this->_get('sid');
        $mod = M('Service');
        $map = array('service_id' => $sid);
        $service     = $mod
                ->join('AS s JOIN ' . C('DB_PREFIX') . 'service_cat as c ON c.cat_id=s.cat_ids ')
                ->where($map)
                ->field('s.*, c.title as cat_name, c.cat_info')
                ->find();
        //评论
        $commit      = M('Comment');
        $map         = array(
            'con_id'   => $sid,
            'con_type' => 'service',
        );
        $count     = $commit
                ->where($map)
                ->count($commit->getPk());

        if ($count > 0) {
            import("ORG.Util.Page");
            //创建分页对象
            $p = new Page($count, 10);
            //分页查询数据

            $voList     = $commit
                    ->where($map)
                    ->order('create_time DESC')
                    ->limit($p->firstRow . ',' . $p->listRows)
                    ->select();
            //分页显示
            $page       = $p->show();
            //模板赋值显示
            $commitlist = array(
                'list'      => $voList,
                'page'      => $page,
                'nowPage'   => $_REQUEST['page'],
                'totalPage' => round($count / $limit),
            );
        }

        //推荐信息
        $roundDZ = _roundDazhe();
        $this->assign('rounddz', $roundDZ);

        $this->assign('comment', $commitlist);
        $this->assign('sv', $service);
        $this->display();
    }

    /**
     * 赞一个 
     */
    public function great() {
        $uid = session('uid');
        $sid = (int) $_REQUEST['sid'];
        if ($uid) {
            if ($_SESSION['greate_time'] + 6 > time()) {
                $this->ajaxReturn(reStatus(false, '频繁操作'));
            }
            $_SESSION['greate_time'] = time();
            $res                     = M('Service')->where(array('service_id' => $sid))->setInc('great_count');
            if ($res) {
                $this->ajaxReturn(reStatus(true));
            } else {
                $this->ajaxReturn(reStatus(false, '操作未成功'));
            }
        } else {
            $this->ajaxReturn(reStatus(false, '您尚未登陆无法操作'));
        }
    }

}
